layui.use(['table', 'admin', 'ax', 'form','layer','ztree','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;


    var layer = layui.layer;
    var $ZTree = layui.ztree;
    var laydate = layui.laydate;
    /**
     * 库位表管理
     */
    var BasicPosition = {
        tableId: "basicPositionTable",
        condition: {
        }
    };

    /**
     * 初始化表格的列
     */
    BasicPosition.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {align: 'center', width: 120, toolbar: '#tableBar', title: '操作'},
            {field: 'positionState', sort: true, title: '状态', templet: '#statusTpl'},
            {field: 'warehouseName', sort: true, title: '所属仓库'},
            {field: 'warehouseNumber', sort: true, title: '仓库编号', hide: true},
            {field: 'positionAreaName', sort: true, title: '所属库区'},
            {field: 'areaNumber', sort: true, title: '库区编号'},
            {field: 'positionNumber', sort: true, title: '库位编号', style: 'color: #2b85e4;font-weight: 600;'},
            {field: 'positionType', sort: true, title: '库位类型'},
            {field: 'positionCondition', sort: true, title: '保管条件', hide: true},
            {field: 'traySize', sort: true, title: '托存量', totalRow: true},
            {field: 'positionLength', sort: true, title: '长', hide: true},
            {field: 'positionWidth', sort: true, title: '宽', hide: true},
            {field: 'positionHeight', sort: true, title: '高', hide: true},
            {field: 'positionXlbId', sort: true, title: '新零帮库位id'},
            {field: 'positionPickingorder', sort: true, title: '拣货顺序'},
            {field: 'positionNotes', sort: true, title: '备注'},
            {field: 'createTime', sort: true, title: '创建时间'},
        ]];
    };
    /**
     * 点击查询按钮
     * positionId
     */
    BasicPosition.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['WarehouseId'] = $("#warehouseId").val();
        queryData['positionAreaId'] = $("#positionAreaId").val();
        queryData['positionType'] = $("#positionType").val();
        queryData['positionProducttype'] = $("#positionProducttype").val();
        queryData['positionState'] = $("#positionState").val();
        queryData['kkw'] = $("#kkw").val();
        queryData['ywdp'] = $("#ywdp").val();
        table.reload(BasicPosition.tableId, {
            where: queryData, page: {curr: 1},
            done: function () {
            }
        });
    };

    // 同步库位信息
    $('#pickWarehouse').click(function () {
        layer.open({
            type: 2,
            title: '同步库位信息',
            area: ['1000px', '750px'],
            anim: 5,
            content: Feng.ctxPath + '/basicPosition/pickWarehouse',
            end: function () {
                table.reload(HxBasicGoods.tableId);
            }
        });
    });



    //批量删除
    $('#btnRevoke').click(function () {
        var checkStatus = table.checkStatus('basicPositionTable');
        data = checkStatus.data;
        if (data.length > 0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
                ids.push(data[i].id);
            }
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/basicPosition/btnRevoke?ids=" + ids, function (data) {
                    if (data.success) {
                        Feng.success("删除成功!");
                        table.reload(BasicPosition.tableId);
                    } else {
                        Feng.error("删除失败!" + data.message + "!");
                    }
                }, function (data) {
                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("是否删除选中明细?", operation);
        } else {
            layer.alert('请至少选中一条数据');
        }
    });

    // 导入
    $('#btnExpimport').click(function () {
        layer.open({
            type: 2,
            title: '库位导入',
            area: ['1000px', '750px'],
            anim: 5,
            content: Feng.ctxPath + '/basicPosition/toimportExcel',
            end: function () {
                table.reload(BasicPosition.tableId);
            }
        });
    });

    // 导入
    $('#btnExpimportUpdata').click(function () {
        layer.open({
            type: 2,
            title: '库位更新导入',
            area: ['1000px', '750px'],
            anim: 5,
            content: Feng.ctxPath + '/basicPosition/toimportExcelUpadate',
            end: function () {
                table.reload(BasicPosition.tableId);
            }
        });
    });

    /**
     * 弹出添加对话框
     */
    BasicPosition.openAddDlg = function () {
        layer.open({
            type: 2,
            title: '添加库位',
            area: ['100%', '100%'],
            anim: 5,
            content: Feng.ctxPath + '/basicPosition/add_hx',
            end: function () {
                table.reload(BasicPosition.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    BasicPosition.exportExcel = function () {
        var checkRows = table.checkStatus(BasicPosition.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };
    
    /**
     * 导出excel按钮
     */
    BasicPosition.exportExcel = function () {
        var checkRows = table.checkStatus(BasicPosition.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
            var queryData = {};
	        queryData['condition'] = $("#condition").val();
	        queryData['WarehouseId'] = $("#warehouseId").val();
	        queryData['positionAreaId'] = $("#positionAreaId").val();
	        queryData['positionType'] = $("#positionType").val();
	        queryData['positionProducttype'] = $("#positionProducttype").val();
        	queryData['positionState'] = $("#positionState").val();
            queryData['kkw'] = $("#kkw").val();
            queryData['ywdp'] = $("#ywdp").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/basicPosition/listAll",
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    BasicPosition.openEditDlg = function (data) {
        layer.open({
            type: 2,
            title: '编辑库位',
            area: ['100%', '100%'],
            anim: 5,
            content: Feng.ctxPath + '/basicPosition/edit_hx?id=' + data.id,
            end: function () {
                table.reload(BasicPosition.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BasicPosition.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/basicPosition/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BasicPosition.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicPosition.tableId,
        url: Feng.ctxPath + '/basicPosition/list',
        page: true,
        limit: 60,
		limits:[60,120,240,360,480,1500,5000],
        height: "full-60",
        cellMinWidth: 100,
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        totalRow: true,//开启合计行
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: BasicPosition.initColumn()
    });
    $('#btnPrint').click(function () {
        var checkStatus = table.checkStatus('basicPositionTable');
        data = checkStatus.data;
        if (data.length > 0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
                ids.push(data[i].positionNumber);
            }
            layer.open({
                type: 2,
                title: '批量打印库位码',
                area: ['900px', '550px'],
                anim: 5,
                content: Feng.ctxPath + '/basicPosition/tobtnqrCode?ids=' + ids,
                end: function () {
                }
            });
        } else {
            layer.alert('请至少选中一条数据');
        }
    });

    $('#btnGoodsPositionCode').click(function () {
        var checkStatus = table.checkStatus('basicPositionTable');
        data = checkStatus.data;
        if (data.length > 0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
                ids.push(data[i].id);
            }
            layer.open({
                type: 2,
                title: '批量打印库位码',
                area: ['900px', '550px'],
                anim: 5,
                content: Feng.ctxPath + '/basicPosition/tobtnGoodsPositionCode?ids=' + ids,
                end: function () {
                }
            });
        } else {
            layer.alert('请至少选中一条数据');
        }
    });

    // 搜索按钮点
    // 击事件
    $('#btnSearch').click(function () {
        BasicPosition.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        BasicPosition.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BasicPosition.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BasicPosition.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BasicPosition.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BasicPosition.onDeleteItem(data);
        }
    });

    //查询库区
    form.on('select(warehouseId)', function () {
        $("#positionAreaId").empty();
        $.ajax({
            url: Feng.ctxPath + '/basicArea/queryArea',
            dataType: "json",   //返回格式为json
            async: false,
            data: {"areaWarehouseId": $("#warehouseId").val()},    //参数值
            type: "post",   //请求方式
            success: function (data) {
                if (data.length > 0) {
                    var str = "<option value=''>选择库区</option>";
                    $.each(data, function (key, value) {
                        str += "<option value='" + value.id + "'>" + value.areaName + "," + value.areaNumber + "</option>";
                    });
                    $("#positionAreaId").html(str);
                }
            }
        });
        form.render('select');
    });

    // 修改参数状态
    form.on('switch(positionState)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

        BasicPosition.changeGradingStatus(id, checked);
    });

    /**
     * 修改参数状态
     *
     * @param id 参数id
     * @param checked 是否选中（true,false），选中就是启用用户，未选中就是禁用用户
     */
    BasicPosition.changeGradingStatus = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/basicPosition/editItemStatus", function (data) {
                if (data.success) {
                    Feng.success("启用成功!");
                } else {
                    Feng.error("启用失败!" + data.message + "!");
                }
            }, function (data) {
                Feng.error("启用失败!");
                table.reload(BasicPosition.tableId);
            });
            ajax.set("positionState", 1);
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/basicPosition/editItemStatus", function (data) {
                if (data.success) {
                    Feng.success("禁用成功!");
                } else {
                    Feng.error("禁用失败!" + data.message + "!");
                }
            }, function (data) {
                Feng.error("禁用失败!" + data.responseJSON.message + "!");
                table.reload(BasicPosition.tableId);
            });
            ajax.set("positionState", 2);
            ajax.set("id", id);
            ajax.start();
        }
    };
});
